<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>详情</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <input type="hidden" id="entityId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">商品</label>
        <div class="layui-input-inline">
            <select id="productId" lay-filter="productId">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">当前库存量</label>
        <div class="layui-input-inline">
            <input type="text" id="realNumber" lay-verify="required|number" placeholder="请输入数字" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">标准线</label>
        <div class="layui-input-inline">
            <input type="text" id="normalNumber" lay-verify="required|number" placeholder="请输入数字" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label">警戒线</label>-->
    <!--        <div class="layui-input-inline">-->
    <!--            <input type="text" id="warnNumber" lay-verify="required|number" placeholder="请输入数字" autocomplete="off"-->
    <!--                   class="layui-input">-->
    <!--        </div>-->
    <!--    </div>-->
    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label">退货总量</label>-->
    <!--        <div class="layui-input-inline">-->
    <!--            <input type="text" id="unusualNumber" lay-verify="required" placeholder="请输入数字" autocomplete="off"-->
    <!--                   class="layui-input">-->
    <!--        </div>-->
    <!--    </div>-->
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <input type="checkbox" id="is_status" name="switch" lay-skin="switch" lay-text="在售|停售" checked value="0">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="entity-submit" id="entity-submit"
               value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit"
               value="确认编辑">
    </div>
</div>
<script src="../../layuiadmin/layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.min.js"></script>
<script>
    if (parent.entityType == "add") {
        //下拉框初始化
        $.ajax({
            url: '/depot/findNoDepot',
            type: 'post',
            dataType: 'json',
            async: false,
            data: {},
            success: function (data) {
                if (eval(data.noDepot).length == 0) {
                    parent.layer.alert('暂时没有需要添加的商品信息，请核对后重试！', {
                        end: function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index);
                        }
                    });
                }
                //将下拉框中添加数据
                var modelList = data.noDepot;
                if (modelList && modelList.length != 0) {
                    for (var i = 0; i < modelList.length; i++) {
                        var option = "<option value=\"" + modelList[i].id + "\"";
                        option += ">" + modelList[i].name + "</option>";  //动态添加数据
                        $('#productId').append(option);
                    }
                }
            },
            error: function () {
                parent.layer.alert("请求数据失败", {
                    end: function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                });
            }
        });
    }
    //根据父级页面判断，如果是修改，则添加默认值
    if (parent.entityType == "toUpdate") {
        $.ajax({
            url: parent.toUpdateURL,
            type: 'post',
            dataType: 'json',
            async: false,//设置阻断
            data: {"id": parent.entityId},
            success: function (data) {
                console.log(data);
                $("#entityId").val(parent.entityId);
                $("#productId").val(data.data.product_id);
                var option = "<option value=\"" + data.data.product_id + "\"";
                option += ">" + data.data.pname + "</option>";  //动态添加数据
                $('#productId').append(option);
                //选中下拉框中的option
                $("#productId").find("option[value=" + data.data.product_id + "]").prop("selected", true);
                $("#realNumber").val(data.data.real_number);
                $("#normalNumber").val(data.data.normal_number);
                // $("#warnNumber").val(data.data.warn_number);
                // $("#unusualNumber").val(data.data.unusual_number);
                if (!data.data.is_status) {
                    $("#is_status").removeAttr("checked");
                }
            }
        });
    }
    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
            var $ = layui.$
                , form = layui.form;
            //监听提交
            form.on('submit(entity-submit)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                //获取id，若无则改为0
                var id = $("#entityId").val();
                if (id == null || id == "") id = 0;
                var iis_status = 0;
                //获取is_status
                if ($("#is_status").attr("checked") == "checked") {
                    iis_status = 1;
                }
                if ($("#productId").val() == "" || $("#productId").val() == null) {
                    parent.layer.alert("请选择商品！");
                    return;
                }
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: parent.entityURL,
                    type: 'post',
                    dataType: 'json',
                    async: false,//设置阻断
                    data: {
                        "id": id,
                        "product_id": $("#productId").val(),
                        "real_number": $("#realNumber").val(),
                        "normal_number": $("#normalNumber").val(),
                        "warn_number": "0",
                        "unusual_number": "0",
                        "is_status": iis_status,
                    },
                    success: function (data) {
                        parent.layer.msg(data.msg, {icon: data.icon, zIndex: layer.zIndex + 100});
                    }
                });
                parent.layui.table.reload('entityTable'); //重载表格
                parent.layer.close(index); //再执行关闭
            });
        }
    )
</script>
</body>
</html>